/* 
 * File: lengtheditor.h
 * Project: QTimeTracker
 * Author: Steve Sindelar, http://stevesindelar.cz
 * Licence: NewBSD licence.
 */

#ifndef LENGTH_EDITOR_H
#define LENGTH_EDITOR_H

#include <QSpinBox>
#include <limits.h>
#include "../QTimeTracker.Core/timespandata.h"

#define LENGTH_TYPE_ID 127

using namespace Core;

namespace Ui
{

	/**
	 * Custom editor for type TimeSpanData.
	 * It internally represents TimeSpanData only using count of seconds.
	 */
	class LengthEditor : public QSpinBox
	{
		Q_OBJECT
		Q_PROPERTY(int seconds READ getSeconds WRITE setSeconds USER true)

	public:
		LengthEditor(QWidget* parent = 0) : QSpinBox(parent) 
		{
			this->setMaximum(INT_MAX);
			this->setSingleStep(60);
		}

		int valueFromText(const QString &text) const
		{
			bool ok;
			TimeSpanData d = TimeSpanData::fromString(text, ok);
			return d.getSecs();
		}

		QString textFromValue(int value) const
		{
			return TimeSpanData::fromSeconds(value).toString();
		}

		void setSeconds(int secs)
		{
			this->setValue(secs);
		}

		int getSeconds()
		{
			return this->value();
		}

	private:
		long seconds;
	};

} //namespace

#endif //LENGTH_EDITOR_H